package com.ppc.controller.employer;


import com.ppc.dto.EmployerTaskDTO;
import com.ppc.dto.EmployerTaskPageQueryDTO;
import com.ppc.result.PageResult;
import com.ppc.result.Result;
import com.ppc.service.EmployerTaskService;
import com.ppc.vo.EmployerWorkerTaskVO;
import com.ppc.vo.TaskDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 雇主订单相关接口
 *
 * @author LENOVO
 * @date 2023/12/20
 */
@RestController
@RequestMapping("/employer/task")
@Slf4j
@Api(tags = "雇主订单相关接口")
public class EmployerTaskController {

  @Autowired
  EmployerTaskService employerTaskService;

  /**
   * 雇主分页查询需求
   *
   * @param employerTaskPageQueryDTO
   * @return
   */
  @GetMapping("/page")
  @ApiOperation("雇主分页查询订单")
  public Result<PageResult> taskPageQuery(EmployerTaskPageQueryDTO employerTaskPageQueryDTO) {
    log.info("雇主分页查询订单，参数为：{}", employerTaskPageQueryDTO);
    PageResult pageResult;
    pageResult = employerTaskService.taskPageQuery(employerTaskPageQueryDTO);
    return Result.success(pageResult);
  }

  /**
   * 雇主查看订单详细信息
   *
   * @param taskId
   * @return
   */
  @GetMapping("/detail")
  @ApiOperation("雇主查看订单详细信息")
  public Result<TaskDetail> detailTask(Long taskId) {
    log.info("雇主查看订单详细信息，参数为：{}", taskId);
    TaskDetail taskDetail = employerTaskService.checkTaskDetail(taskId);
    return Result.success(taskDetail);
  }

  /**
   * 雇主查看工人提交的结果
   *
   * @param taskId
   * @return
   */
  @GetMapping("/result")
  @ApiOperation("雇主查看工人提交的结果")
  public Result<EmployerWorkerTaskVO> empWorkerTask(Long taskId) {
    log.info("雇主查看员工提交的结果：参数为：{}", taskId);
    EmployerWorkerTaskVO employerWorkerTaskVO = employerTaskService.checkWorkerTask(taskId);
    return Result.success(employerWorkerTaskVO);
  }

  /**
   * 雇主申请修改订单
   *
   * @param employerTaskDTO
   * @return
   */
  @PostMapping("/change")
  @ApiOperation("雇主修改订单")
  public Result updateTask(EmployerTaskDTO employerTaskDTO) {
    log.info("雇主修改订单信息：参数为：{}", employerTaskDTO);
    employerTaskService.updateTask(employerTaskDTO);
    employerTaskService.createClaim(employerTaskDTO.getTaskId(), 1,
        employerTaskDTO.getClaimReason());
    employerTaskService.updateTaskStatus(employerTaskDTO.getTaskId());
    return Result.success();
  }

  /**
   * 雇主申请删除订单
   *
   * @param taskId
   * @param claimReason
   * @return
   */
  @PostMapping("/drop")
  @ApiOperation("雇主删除订单")
  public Result dropTask(Long taskId, String claimReason) {
    log.info("雇主删除订单：参数为：{} {}", taskId, claimReason);
    employerTaskService.createClaim(taskId, 2, claimReason);
    employerTaskService.updateTaskStatus(taskId);
    return Result.success();
  }

  /**
   * 雇主不满意订单
   *
   * @param taskId
   * @param claimReason
   * @return {@link Result}
   */
  @PostMapping("/notSatisfied")
  @ApiOperation("雇主不满意订单")
  public Result notSatisfiedTask(Long taskId, String claimReason) {
    log.info("雇主删除订单：参数为：{} {}", taskId, claimReason);
    employerTaskService.createClaim(taskId, 3, claimReason);
    employerTaskService.updateTaskStatus(taskId);
    return Result.success();
  }

  /**
   * 雇主确认订单
   *
   * @param taskId
   * @return
   */
  @GetMapping("/confirm")
  @ApiOperation("雇主确认订单")
  public Result confirmTask(Long taskId) {
    log.info("雇主确认订单：参数为：{}", taskId);
    employerTaskService.confirmTask(taskId);
    return Result.success();
  }

  /**
   * 工人钱包金额变化
   */
  @PostMapping("/workererMoney")
  @ApiOperation(value = "更改工人钱包金额")
  public Result workerWallet(Long taskId) {
    log.info("工人钱包金额变化：{}", taskId);
    employerTaskService.updateWorkerWallet(taskId);
    return Result.success();
  }


  /**
   * 雇主确认订单后工人钱包的变化记录
   */
  @PostMapping("/workerMoneyRecording")
  @ApiOperation(value = "工人钱包金额变化记录")
  public Result createWorkerMoneyRecording(Long taskId) {
    log.info("工人钱包金额变化记录：{}", taskId);
    employerTaskService.createWorkerMoneyRecording(taskId);
    return Result.success();
  }

  /**
   * 雇主付款
   *
   * @param taskId
   * @return
   */
  @PostMapping("/employerPay")
  @ApiOperation(value = "雇主付款")
  public Result employerPay(Long taskId) {
    log.info("雇主付款：{}", taskId);
    employerTaskService.updateEmployerWallet(taskId);
    employerTaskService.createEmployerMoneyRecording(taskId);
    return Result.success();
  }

  /**
   * 雇主发布需求
   *
   * @param taskId
   * @return
   */
  @GetMapping("/publish")
  @ApiOperation("雇主发布需求")
  public Result<T> publishRequire(Long taskId) {
    log.info("雇主发布需求:参数为：{}", taskId);
    employerTaskService.publishRequire(taskId);
    return Result.success();
  }

}
